package nskhan.controls;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import nskhan.controls.Canvas.Mode;
import nskhan.utilities.StrokeFile;

public class SketchCanvas extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JButton btnOpenFile = null;
	private JButton btnSaveFile = null;
	private JButton btnDraw = null;
	private JButton btnClear = null;

	private JPanel radioButtonList = null;
	private Canvas canvas = null;

	public SketchCanvas() {
		super();
		initialize();
	}

	private void initialize() {
		this.setLayout(null);
		this.setSize(800, 530);

		this.add(getCanvas());
		this.add(getBtnOpen());
		this.add(getBtnSave());
		this.add(getBtnDraw());
		this.add(getBtnClear());
		this.add(getRadioButtonList());
	}

	private JButton getBtnOpen() {
		if (btnOpenFile == null) {
			btnOpenFile = new JButton();
			btnOpenFile.setText("Open  Sketch");
			btnOpenFile.setSize(110, 20);
			btnOpenFile.setLocation(650, 30);
			btnOpenFile.addActionListener(this);
		}
		return btnOpenFile;
	}

	private JButton getBtnSave() {
		if (btnSaveFile == null) {
			btnSaveFile = new JButton();
			btnSaveFile.setText("Save Sketch");
			btnSaveFile.setActionCommand("Save");
			btnSaveFile.setSize(110, 20);
			btnSaveFile.setLocation(650, 60);
			btnSaveFile.addActionListener(this);
		}
		return btnSaveFile;
	}

	private JButton getBtnDraw() {
		if (btnDraw == null) {
			btnDraw = new JButton();
			btnDraw.setText("Draw Sketch");
			btnDraw.setSize(110, 20);
			btnDraw.setLocation(650, 90);
			btnDraw.addActionListener(this);
		}
		return btnDraw;
	}

	private JButton getBtnClear() {
		if (btnClear == null) {
			btnClear = new JButton();
			btnClear.setText("Clear Canvas");
			btnClear.setActionCommand("Clear");
			btnClear.addActionListener(this);
			btnClear.setSize(110, 20);
			btnClear.setLocation(650, 120);
		}
		return btnClear;
	}

	private JPanel getRadioButtonList() {

		if (radioButtonList == null) {

			JRadioButton rdoDrawingMode = new JRadioButton();
			rdoDrawingMode.addActionListener(this);
			rdoDrawingMode.setActionCommand("DrawingMode");
			rdoDrawingMode.setSelected(true);
			rdoDrawingMode.setText("Drawing Mode");

			JRadioButton rdoLabelingMode = new JRadioButton();
			rdoLabelingMode.addActionListener(this);
			rdoLabelingMode.setActionCommand("LabelingMode");
			rdoLabelingMode.setText("Labeling Mode");

			ButtonGroup radioButtonGroup = new ButtonGroup();
			radioButtonGroup.add(rdoDrawingMode);
			radioButtonGroup.add(rdoLabelingMode);

			radioButtonList = new JPanel(new FlowLayout());
			radioButtonList.add(rdoDrawingMode);
			radioButtonList.add(rdoLabelingMode);

			radioButtonList.setSize(230, 50);
			radioButtonList.setLocation(0, 0);
		}

		return radioButtonList;

	}

	private Canvas getCanvas() {
		if (canvas == null) {
			canvas = new Canvas();
			canvas.setSize(640, 480);
			canvas.setLocation(0, 30);
		}
		return canvas;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getActionCommand() == "DrawingMode") {
			canvas.setMode(Mode.drawing);
		} else if (e.getActionCommand() == "LabelingMode") {
			canvas.setMode(Mode.labeling);
		} else if (e.getActionCommand() == "Clear") {
			canvas.clearCanvas();

		}
		if (e.getActionCommand() == "Save") {			
				StrokeFile strokeFile = canvas.getStrokeFile();				
				strokeFile.saveStrokeFile();
		}
	}

}
